import {storeBindingsBehavior} from "mobx-miniprogram-bindings";
import base from "@src/store/base";

Component({
  behaviors: [storeBindingsBehavior],
  storeBindings: [
    {
      store: base,
      fields: ['themeColor', 'themeTitleColor']
    }
  ],
  options: {
    addGlobalClass: true,
  },

  properties: {},
  data: {
    reject: null,
    resolve: null,
    dialogShow: false,
    text: '',
    title: '备注信息',
    maxLen: 50,
    nowCursor: 0,
  },
  methods: {
    open(options) {
      let {title, text} = options;
      if (!text) {
        text = "";
      }
      if (!title) {
        text = "输入";
      }
      return new Promise(((resolve, reject) => {
        this.setData({
          title: title, text: text, nowCursor: text.length,
          resolve: resolve, reject: reject, dialogShow: true
        });
      }))
    },
    handleInput(e) {
      this.setData({
        nowCursor: e.detail.cursor,
        text: e.detail.value
      })
    },
    handleClose() {
      this.setData({dialogShow: false, resolve: null, reject: null})
    },
    handleFinish() {
      let resolve = this.data.resolve;
      let text = this.data.text;
      this.handleClose();
      if (resolve) {resolve(text);}
    }
  }
});
